﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;

namespace SSP
{
    class SSPLevelProcessor
    {
        SSPLevelDefinition level;

        public SSPLevelProcessor()
        {
            level = new SSPLevelDefinition();
        }

        public SSPLevelDefinition Read(string fileName)
        {
            using (XmlReader reader = XmlReader.Create(fileName))
            {
                while (reader.Read())
                {
                    if (reader.IsStartElement())
                    {
                        this.elementProcessor(reader);

                        if (reader.IsEmptyElement)
                        {
                        }
                        else
                        {
                            reader.Read(); // Read the start tag.
                            if (reader.IsStartElement())  // Handle nested elements.
                            {
                                this.elementProcessor(reader);
                            }
                        }
                    }
                }
            }

            return level;
        }

        void elementProcessor(XmlReader reader)
        {
            if (reader.Name.Equals("Level"))
            {
                if (reader.HasAttributes)
                {
                    //Console.WriteLine("Level Switch");
                    this.addLevelData(reader[0]);
                }
            }
            else if (reader.Name.Equals("Element"))
            {
                if (reader.HasAttributes)
                {
                    //Console.WriteLine("Element Switch");
                    this.addElement(reader[0], reader[1], reader[2], reader[3]);
                    //Console.WriteLine("Element {0}", reader[0]);
                }
            }
            else if (reader.Name.Equals("Spawn"))
            {
                if (reader.HasAttributes)
                {
                    this.addSpawn(reader[0], reader[1], reader[2]);
                    //Console.WriteLine("Spawn Switch");
                }
            }
            else if (reader.Name.Equals("Texture"))
            {
                if (reader.HasAttributes)
                {
                    this.addTexture(reader[0], reader[1]);
                    //Console.WriteLine("Texture Switch");
                }
            }
            else
            {
            }
        }

        public void addLevelData(string l)
        {
            level.length = Int32.Parse(l);
        }

        public void addElement(string x, string y, string scale, string texture)
        {
            level.addBackgroundElement(new SSPBackgroundElement(Int32.Parse(x), Int32.Parse(y), float.Parse(scale), texture));
            
        }

        public void addSpawn(string name, string x, string y)
        {
            level.addSpawnElement(name, Int32.Parse(x), Int32.Parse(y));
        }

        public void addTexture(string tag, string path)
        {
            level.addTextureElement(tag, path);
        }
    }
}
